<template>
  <div class="app-container bg-gray">
    <el-row class="box-1">
      <el-col :span="24">
        <el-card>
          <div class="header">
            <div>
              <el-input v-model="searchTerms.searchValue" placeholder="请输入内容" class="search-input" @change="getData"/>
              <el-button type="success" class="bg-green" icon="el-icon-search" @click="getData">搜索</el-button>
            </div>
          </div>
          <el-table v-loading="tableLoading" :data="tableData" :max-height="tableMaxHeight" border highlight-current-row @sort-change="handleSortChange">
            <el-table-column prop="id" label="ID" min-width="65" header-align="center" align="center" sortable />
            <el-table-column prop="name" label="工单提交人" min-width="120"/>
            <el-table-column prop="description" label="问题描述" min-width="300"/>
            <el-table-column prop="type" label="工单类型" min-width="120">
              <template slot-scope="scope">
                {{ scope.row.type==1?'需求':scope.row.type==2?'投诉':'其他' }}
              </template>
            </el-table-column>
            <el-table-column prop="status" label="工单状态" min-width="120" header-align="center" align="center">
              <template slot-scope="scope">
                <el-tag :type="scope.row.status==0?'danger':'success'">{{ scope.row.status==0?'未处理':'已处理' }}</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="updated_at" label="更新时间" min-width="160" header-align="center" align="center" sortable />
            <el-table-column prop="remarks" label="处理答复" min-width="280"/>
            <el-table-column v-if="queryMatch(118)" label="操作" min-width="120" header-align="center" align="center">
              <template slot-scope="scope">
                <el-button type="success" size="small" icon="el-icon-edit" class="bg-green" @click="toEdit(scope.row)" >处理</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            :current-page.sync="currentPage"
            :page-size="pageSize"
            :page-sizes="pageSizes"
            :total="total"
            class="pagination"
            layout="total, sizes, prev, pager, next, jumper"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"/>
        </el-card>
      </el-col>
    </el-row>

    <el-dialog :modal-append-to-body="false" :visible.sync="dialogVisible" :title="form.title" width="500px" class="dialog" center @close="resetDialog('form')">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="工单提交人">
          <span>{{ form.name }}</span>
        </el-form-item>
        <el-form-item label="工单描述">
          <span>{{ form.description }}</span>
        </el-form-item>
        <el-form-item label="产品图片">
          <img v-for="(item, index) in form.pic_list_url" :key="index" :src="imageUrl+item" alt="" @click="handlePictureCardPreview(item)">
          <el-dialog :visible.sync="imageDialogVisible" append-to-body width="80%">
            <img :src="dialogImageUrl" style="width: 100%;" alt="">
          </el-dialog>
        </el-form-item>
        <el-form-item label="其他信息">
          <div>产品名称：{{ form.product }}</div>
          <div>微购儿账号：{{ form.user_account }}</div>
          <div>微购儿密码：{{ form.user_password }}</div>
        </el-form-item>
        <el-form-item label="处理答复" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" rows="4" autofocus/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" class="bg-green" @click="submitDialog('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { handleSortChange, handleSizeChange, handleCurrentChange, columnCheck, parseTime } from '../../utils/table'
export default {
  data() {
    return {
      // imageUrl: this.$store.getters.serverUrl,
imageUrl: '',
      searchTerms: {
        searchValue: ''
      },

      // 表格相关数据
      tableMaxHeight: document.documentElement.clientHeight - 280,
      // 表格相关参数
      tableLoading: false,
      currentPage: 1,
      pageSize: 10,
      pageSizes: [5, 10, 20, 50, 100, 1000],
      total: 0,
      tableData: [],

      // 弹出框相关数据
      dialogVisible: false,
      imageDialogVisible: false,
      dialogImageUrl: '', // 预览的图片
      form: {
        title: '',
        remarks: ''
      },
      rules: {
        remarks: [{ required: true, message: '请输入答复', trigger: 'change' }]
      }
    }
  },
  watch: {},
  created() {
    this.getData()
  },

  methods: {
    handleCurrentChange, handleSizeChange, handleSortChange, columnCheck, parseTime,
    getData(order) {
      this.tableLoading = true
      order || (order = {})
      const url = 'workorder/getDataList'
      const data = {
        page_no: this.currentPage,
        page_size: this.pageSize,
        search: this.searchTerms.searchValue
      }
      order.sortName && (data.sortName = order.sortName)
      order.sortOrder && (data.sortOrder = order.sortOrder)
      this.$store.dispatch('GetConnect', { url, data }).then(res => {
        this.tableData = res.data.rows
        for (const i in res.data.rows) {
          this.tableData[i].pic_list_url = JSON.parse(res.data.rows[i].pic_list)
        }
        this.total = res.data.total
        this.tableLoading = false
      }).catch((e) => {
        this.tableLoading = false
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    /**
       * 添加团队弹出框和修改团队弹出窗
       * row: 当前行的数据
       */
    toEdit(row) {
      this.form = {};
      this.dialogVisible = true;
      this.form = row;
      this.form.title = '工单处理';
      const extend = JSON.parse(row.extend);
      Object.assign(this.form, extend);
    },

    /**
       * 弹窗表单提交方法,通过判断有没有form_id执行添加或编辑操作
       * formName: form表单中ref的值,即表单名
       */
    submitDialog(formName) {
      this.$refs[formName].validate((valid) => { // 表单验证
        if (valid) {
          const url = '/workorder/update'
          const data = this.form
          this.$store.dispatch('GetConnect', { url, data }).then(res => {
            this.dialogVisible = false
            this.$message.success(res.msg)
            this.getData()
          }).catch(e => {
            this.$message.error(e.msg + ',请刷新或联系管理员!')
          })
        }
      })
    },
    /**
       * 表单查询条件重置方法,清除数据及验证信息
       * formName: form表单中ref的值,即表单名
       */
    resetDialog(formName) {
      this.$refs[formName].resetFields()
    },

    handlePictureCardPreview(file) {
      this.dialogImageUrl = this.imageUrl + file
      this.imageDialogVisible = true
    }
  }
}
</script>

<style lang="scss" type="text/scss" scoped>
  @import '../../styles/index.scss';
  .box-1 {
    .header {
      @include flex-row(center, space-between);
      padding-bottom: 16px;
      .search-input {
        width: 240px;
      }
    }
  }
  .dialog {
    .el-input {
      width: 80%;
    }
    img{
      width:25%;
      margin-right: 5%;
    }
  }
</style>
